﻿$(document).ready(
	function () {

		$.each($('.action'), function () {
			var text = $(this).text().trim();
			if (text == '立即导入') {
				$(this).bind("click", { fun: Import, title: Title }, ShowMiniPop);
			}
		});

		$.each($('#pb_form .action'), function () {
			var txt = $(this).text();
			if (txt == '例子') {
				$(this).bind("click", function () {
					$('#tb_dicts').val('work\r\nfriends\r\ngo\r\nhome\r\npicture\r\nswron\r\ndusk\r\nparturition\r\ngrumpy\r\nscent\r\nheart\r\nsum up\r\ndeflate\r\nexplicite');
				});
			}
			else if (txt == '修改') {
				$(this).bind("click", { fun: Edit, check: Check, title: "", argus: { again: false} }, ShowMiniHint);
			}
			else if (txt == '取消') {
				if ($(this).attr('id') == 'pb_cancel') {
					$(this).bind("click", function () {
						window.history.go(-1);
					});
				}
			}
		});

		$('#notedita').bind("click", function () {
			$(this).addClass('currect');
			$('#editeda').removeClass('currect');
			$('#alla').removeClass('currect');

			var length = jsondata.dicts.length;

			for (var i = 0; i < length; i++) {
				var dict = jsondata.dicts[i];
				if (oVoc.status < 0 && dict.ph1 == '') {
					$('#li_' + dict.id).show();
				}
				else {
					$('#li_' + dict.id).hide();
				}
			}
		});

		$('#alla').bind("click", function () {
			$(this).addClass('currect');
			$('#editeda').removeClass('currect');
			$('#notedita').removeClass('currect');

			var length = jsondata.dicts.length;

			for (var i = 0; i < length; i++) {
				var dict = jsondata.dicts[i];
				$('#li_' + dict.id).show();
			}
		});

		$('#editeda').bind("click", function () {
			$(this).addClass('currect');
			$('#notedita').removeClass('currect');
			$('#alla').removeClass('currect');

			var length = jsondata.dicts.length;

			for (var i = 0; i < length; i++) {
				var dict = jsondata.dicts[i];
				if (oVoc.status < 0 && dict.ph1 == '') {
					$('#li_' + dict.id).hide();
				}
				else {
					$('#li_' + dict.id).show();
				}
			}
		});


		if (parseInt($('#nknowcount').html()) == 0) {
			$('#nknow').hide();
			$('#hknow').hide();
		}
		else {
			$('#nknow').show();
			$('#hknow').show();
		}

		$('#nknow').bind("click", ShowNKnow);
		$('#hknow').bind("click", ShowHKnow);
		$('#alldict').bind("click", ShowAllDict);
	}
);

function EditDict(argus) {
	var did = argus.data.did;
	
	$.ajax({
		url: '/Submit/submit.ashx?page=dict&did=' + did + '&action=020&rnd=' + Math.random(),
		type: 'POST',
		timeout: 5000,
		dataType: "json",
		data: { 'ph': $('#paraphrase').val(), 'ct': $('#commentate').val() },
		error: function () {
			AddErrMiniHint('操作失败,请重试');
		},
		success: function (json) {
			if (json.code == 0) {
				AddMiniHint('操作成功！');
				HideMiniPop(1000);
				$('#div_' + did).html(GetPh($('#paraphrase').val()));
			}
			else {
				AddErrMiniHint('操作失败,请重试');
			}
		}
	});
}

function EditDictTitle(fun,a) {
	var title;
	var did = a.attr('data-id');
	title = '<div class="pb_form">';
	title += '<span class="spell">' + oDict[did].spell + '</span>';
	title += '	<h3>解释<span></span></h3>'
	title += '	<textarea id="paraphrase"cols="20" rows="2">' + oDict[did].ph1 + '</textarea>'
	title += '	<h3>其它<span>（填你想填的）</span></h3>'
	title += '	<textarea id="commentate"cols="20" rows="2"></textarea>'
	title += '</div>'
	fun(title);
}

function Edit(argus) {
	$.ajax({
		url: '/Submit/submit.ashx?page=voc&vid=' + oVoc.vid + '&action=018&rnd=' + Math.random(),
		type: 'POST',
		timeout: 5000,
		dataType: "json",
		data: { 'name': $('#tb_title').val(), 'desc': $('#tb_instruction').val(), 'dicts': $('#tb_dicts').val(), 'sid': $("input[name='stations']:checked").val() },
		error: function () {
			AddErrMiniHint('操作失败,请重试');
		},
		success: function (json) {
			if (json.code == 0) {
				AddMiniHint('操作成功！');

				HideMiniPop(1000);

				setTimeout(function () { window.location = "/edit.aspx?vid=" + oVoc.vid; }, 1000);
			}
			else {
				AddErrMiniHint('操作失败,请重试');
			}
		}
	});
}

function Check() {
	if ($('#tb_title').val().trim() == '' || $('#tb_dicts').val().trim() == '') {
		return '<strong>标题</strong> 和 <strong>单词列表</strong> 不能为空!';
	}
	else {
		return '';
	}
}

function Import() {
	var vid = $("input[name='imports']:checked").val();

	$.ajax({
		url: '/Submit/submit.ashx?page=voc&action=017&vid=' + oVoc.vid + '&fvid=' + vid + '&rnd=' + Math.random(),
		type: 'GET',
		timeout: 5000,
		dataType: "json",
		error: function () {
			AddErrMiniHint('操作失败,请重试');
		},
		success: function (json) {
			if (json.code >= 0) {
				AddSucceedMiniHint('成功导入' + json.code + '个单词的解释！');
			}
			else {
				AddErrMiniHint('操作失败,请重试');
			}
		}
	});
}

function Title(fun) {
	$.ajax({
		url: '/Submit/submit.ashx?page=voc&action=016&vid=' + oVoc.vid + '&rnd=' + Math.random(),
		type: 'GET',
		timeout: 5000,
		dataType: "json",
		error: function () {
			fun('操作失败，请重试');
		},
		success: function (json) {
			if (json.code >= 0) {
				var h = '<div id="fromvoc"><h3>请选择来源词汇表</h3><ul>';
				for (var i = 0; i < json.obj.length; i++) {
					var o = json.obj[i];
					h += '<li><input type="radio" name="imports" value="' + o.vid + '">' + o.name + '(单词：' + o.dictcount + '个)</li>';
				}
				h += '<li><input type="radio" name="imports" value="0">NO.<input style="width:60px;" type="text" value="">(<span>输入编码即可</span>)</li>';
				h += '</ul><div>';
				fun(h);
			}
			else {
				fun('操作失败，请重试');
			}
		}
	});
}


var dictlist = [];
var oDict = {};
function BuildList() {
	var length = jsondata.dicts.length;

	for (var i = 0; i < length; i++) {
		var dict = jsondata.dicts[i];
		oDict[dict.id] = dict;
		dictlist[i] = length - i - 1;
		BuildDict(dict, i, jsondata.vid);
	}
}

function BuildDict(dict, num, vid) {
	var sb = new StringBuffer();
	var did = dict.id;
	var Spell = dict.spell;
	var Pronunciation = dict.pron;
	var ListIndex = dict.li;

	var ishide = 'hide';
	if (dict.ph1 == '') {
		ishide = '';
	}

	sb.Append('<li id="li_' + did + '" class="dict ' + ishide + '">');
	sb.Append(' <div class="spell">');
	sb.Append('     <label class="num"> ' + (num + 1) + '. </label>');
	sb.Append('     <span class="spell">' + Spell + '</span>');
	sb.Append('     <span class="dict-act"><a data-id="' + did + '" id="edit_' + did + '" class="action">编辑</a></span>');
	if (Pronunciation != "") {
		sb.Append(' <span style="visibility:hidden"> … </span>');
		sb.Append(' <span class="pron">' + Pronunciation.replace(/\|/gim, '<img border="0" src="/Common/images/compart.png" class="compart"/>') + '</span>');
	}

	if (Spell.indexOf(" ") == -1 && Spell.indexOf("…") == -1) {
		sb.Append(' <span style="visibility:hidden"> … </span>');
	}
	sb.Append(" </div>");
	if (dict.ph1 != '') {
		sb.Append('<div id="div_' + did + '" class="ph">' + GetPh(dict.ph1) + '</div>');
	}
	else {
		sb.Append('<div id="div_' + did + '" class="ph"></div>');
	}

	sb.Append('</li>');

	$('#ullist').append(sb.toString());

	$('#edit_' + did).bind("click", { fun: EditDict, argus: { did: did }, title: EditDictTitle }, ShowMiniPop);
}

function GetPh(x) {
	x = BlodPh(x);
	x = x.replace(/\[en\](.*?)\[\/en\]/gim, '<div style="font-size:14px;">$1</div>')
	x = x.replace(/\[cn\](.*?)\[\/cn\]/gim, "<div>$1</div>");
	return x;
}

function CheckLen(e) {
	var str = $(this).val();
	var len = str.replace(/[^\x00-\xff]/g, "**").length;
	$('#rlen_' + e.data.did).text(parseInt(e.data.len) - len)
}


function ShowHKnow() {
	$.each($('#mastery a'), function () {
		$(this).removeClass('current');
	});
	$(this).addClass('current');
	$.each($('#ullist li'), function () {
		if ($(this).attr('class') == '') {
			$(this).show();
		}
		else {
			$(this).hide();
		}
	});
}

function ShowNKnow() {
	$.each($('#mastery a'), function () {
		$(this).removeClass('current');
	});
	$(this).addClass('current');
	$.each($('#ullist li'), function () {
		if ($(this).attr('class') == '') {
			$(this).hide();
		}
		else {
			$(this).show();
		}
	});
}

function ShowAllDict() {
	$.each($('#mastery a'), function () {
		$(this).removeClass('current');
	});
	$(this).addClass('current');
	$.each($('#ullist li'), function () {
		$(this).show();
	});
}